using System.Collections.Specialized;
using System.ComponentModel;
using EnvDTE;
using Microsoft.Practices.RecipeFramework.Library.Converters;
using Microsoft.Practices.RecipeFramework.Library.Editors;
using Microsoft.Practices.WizardFramework;
using VSSDKAssist.Assets.Converters;
using VSSDKAssist.Recipes.Common.CustomPages;
using VSSDKAssist.Recipes.Common.Helpers;

namespace VSSDKAssist.Recipes.CreateVSCustomTool
{
	/// <summary>
    /// Custom page for CreateVSCustomTool recipe
	/// </summary>
	public partial class CreateVSCustomToolCP1 : WizardPageBase
	{
		/// <summary>
        /// Initializes a new instance of the <see cref="CreateVSCustomToolCP1"/> class.
		/// </summary>
		public CreateVSCustomToolCP1()
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="CreateVSCustomToolCP1"/> class.
		/// </summary>
		/// <param name="wizard">The <see cref="T:Microsoft.Practices.WizardFramework.WizardForm"/>that will host the new page.</param>
        public CreateVSCustomToolCP1(WizardForm wizard)
			: base(wizard)
		{
			InitializeComponent();
		}
        
		[RecipeArgument]
        public string CodeGeneratorName
		{
			get { return (string)veCustomToolName.Value; }
			set
			{
			}
		}

        [RecipeArgument]
        public string CodeGeneratorDescription
        {
            get { return (string)veCustomToolDescription.Value; }
            set
            {
            }
        }

        [RecipeArgument]
        public string AssociatedFileExtension
        {
            get { return (string)veAssociatedExtension.Value; }
            set
            {
                if(value != null)
                {
                    veAssociatedExtension.TextBox.Text = value;
                }
            }
        }

		/// <summary>
		/// Gets or sets the destination folder.
		/// </summary>
		/// <value>The destination folder.</value>
		[RecipeArgument]
		public string DestinationFolder
		{
			get { return veDestinationFolder.Value.ToString(); }
			set
			{
				if(value != null)
				{
					veDestinationFolder.TextBox.Text = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the site of the control.
		/// </summary>
		/// <value></value>
		/// <returns>The <see cref="T:System.ComponentModel.ISite"></see> associated with the <see cref="T:System.Windows.Forms.Control"></see>, if any.</returns>
		/// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
		public override ISite Site
		{
			get
			{
				return base.Site;
			}
			set
			{
				base.Site = value;
				if(value != null)
				{
					this.veCustomToolName.BeginInit();
					this.veCustomToolName.ValueType = typeof(string);
					base.Site.Container.Add(veCustomToolName);
					this.veCustomToolName.ConverterInstance = new CSharpIdentifierConverter();
					this.veCustomToolName.EndInit();

                    this.veCustomToolDescription.BeginInit();
                    this.veCustomToolDescription.ValueType = typeof(string);
                    base.Site.Container.Add(veCustomToolDescription);
                    this.veCustomToolDescription.EndInit();

                    this.veAssociatedExtension.BeginInit();
                    this.veAssociatedExtension.ValueType = typeof(string);
                    this.veAssociatedExtension.ConverterInstance = new FileExtensionConverter();
                    base.Site.Container.Add(veAssociatedExtension);
                    this.veAssociatedExtension.EndInit();

					this.veDestinationFolder.BeginInit();
					this.veDestinationFolder.ValueType = typeof(string);
					base.Site.Container.Add(veDestinationFolder);
					this.veDestinationFolder.ConverterInstance = new CSharpIdentifierConverter();
					this.veDestinationFolder.EndInit();
				}
			}
		}

		private void veDestinationFolder_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
        {
            if(veDestinationFolder.ConverterInstance != null)
            {
                errorProvider.SetError(veDestinationFolder, string.Empty);

                if(veDestinationFolder.Value != null)
                {
                    DictionaryService.SetValue("DestinationFolder", veDestinationFolder.Value.ToString());
                }
                else
                {
                    DictionaryService.SetValue("DestinationFolder", null);
                    errorProvider.SetError(veDestinationFolder, Properties.Resources.InvalidValue);
                }
            }
            else
            {
                DictionaryService.SetValue("DestinationFolder", veDestinationFolder.Value.ToString());
            }
        }

        private void veCustomToolDescription_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
        {
            string customToolDescription = veCustomToolDescription.Value as string;
            DictionaryService.SetValue("CodeGeneratorDescription", customToolDescription);
        }

        private void veAssociatedExtension_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
        {
            if(veAssociatedExtension.ConverterInstance != null)
            {
                errorProvider.SetError(veAssociatedExtension, string.Empty);

                if(veAssociatedExtension.Value != null)
                {
                    string associatedExtension = veAssociatedExtension.Value as string;
                    DictionaryService.SetValue("AssociatedFileExtension", associatedExtension);
                }
                else
                {
                    DictionaryService.SetValue("AssociatedFileExtension", null);
                    errorProvider.SetError(veAssociatedExtension, Properties.Resources.InvalidValue);
                }
            }
            else
            {
                DictionaryService.SetValue("AssociatedFileExtension", veAssociatedExtension.Value.ToString());
            }
        }

        private void veCustomToolName_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
        {
            if(veCustomToolName.ConverterInstance != null)
            {
                errorProvider.SetError(veCustomToolName, string.Empty);

                if(veCustomToolName.Value != null)
                {
                    if(CustomPageHelper.ProjectItemExists(
                        DictionaryService,
                        "VSPackageProject",
                        "DestinationFolder",
                        string.Concat(veCustomToolName.Value.ToString(), ".", DictionaryService.GetValue("LanguageExtension").ToString())))
                    {
                        DictionaryService.SetValue("CodeGeneratorName", null);
                        errorProvider.SetError(veCustomToolName, Properties.Resources.InvalidValue);
                    }
                    else
                    {
                        DictionaryService.SetValue("CodeGeneratorName", veCustomToolName.Value.ToString());
                    }
                }
                else
                {
                    DictionaryService.SetValue("CodeGeneratorName", null);
                    errorProvider.SetError(veCustomToolName, Properties.Resources.InvalidValue);
                }
            }
            else
            {
                DictionaryService.SetValue("CodeGeneratorName", veCustomToolName.Value.ToString());
            }
        }
	}
}